#coding=utf-8
#Boa:Dialog:DlgRoute

import wx

from MyListCtrl import MyListCtrlRoute

def create(parent):
    return DlgRoute(parent)

[wxID_DLGROUTE, wxID_DLGROUTEBTNADD, wxID_DLGROUTEBTNDEL, 
] = [wx.NewId() for _init_ctrls in range(3)]

class DlgRoute(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGROUTE, name=u'DlgRoute',
              parent=prnt, pos=wx.Point(328, 175), size=wx.Size(848, 595),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u76f4\u9001\u8def\u7ebf')
        self.SetClientSize(wx.Size(840, 561))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Center(wx.BOTH)

        self.btnAdd = wx.Button(id=wxID_DLGROUTEBTNADD,
              label=u'\u65b0\u589e\u8def\u7ebf', name=u'btnAdd', parent=self,
              pos=wx.Point(8, 8), size=wx.Size(93, 26), style=0)
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_DLGROUTEBTNADD)

        self.btnDel = wx.Button(id=wxID_DLGROUTEBTNDEL,
              label=u'\u5220\u9664\u9009\u4e2d', name=u'btnDel', parent=self,
              pos=wx.Point(120, 8), size=wx.Size(93, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGROUTEBTNDEL)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.lc = MyListCtrlRoute(self)
        self.lc.SetSize(wx.Size(700, 400))
        self.lc.Move(wx.Point(8, 40))
        self.lc.refresh()
        self.lc.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)

    def getSelectObj(self):
        return self.m_route

    def OnLeftDClick(self, event):
        self.m_route = self.lc.getSelectObj()
        self.EndModal(wx.ID_OK)
        
    def OnBtnAddButton(self, event):
        import DlgRouteEditor
        
        dlg = DlgRouteEditor.create(self)
        if (dlg.ShowModal() != wx.ID_OK):
            return
        
        self.lc.refresh()

    def OnBtnDelButton(self, event):
        import dbmngr
        
        routeObj = self.lc.getSelectObj()
        try:
            dbmngr.dbmngr.beginTrans()
            routeObj.deepDelete()
            dbmngr.dbmngr.commitTrans()
        
        except Exception as e:
            dbmngr.dbmngr.rollbackTrans(e)

        self.lc.refresh()
    
